package jude.java.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class FileBuilder {

	public static void renderFile(Document document, String fileName)
			throws IOException {
		FileWriter out = new FileWriter(fileName);

		// 简易方式
		// document.write(out);
		// out.close();

		// 美化格式
		OutputFormat format = OutputFormat.createPrettyPrint();
		// OutputFormat format = OutputFormat.createCompactFormat();
		XMLWriter writer = new XMLWriter(out, format);
		writer.write(document);
		writer.close();

		// 压缩格式
		// OutputFormat format = OutputFormat.createCompactFormat();
		// XMLWriter writer = new XMLWriter( out, format );
		// writer.write( document );
		// writer.close();
	}

	public static void renderFile(Document document, String filePath,
			String encoding) throws IOException {
		document.setXMLEncoding(encoding);
		FileOutputStream out = new FileOutputStream(new File(filePath));
		out.write(document.asXML().getBytes(encoding));
		out.close();
	}

}
